package top.lenjon.lib.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lenjon.lib.annotation.MailSign;
import top.lenjon.lib.entity.Book;
import top.lenjon.lib.entity.Log;
import top.lenjon.lib.entity.UserDetail;
import top.lenjon.lib.model.ResponseModel;
import top.lenjon.lib.service.IAdminService;
import top.lenjon.lib.service.IBookService;
import top.lenjon.lib.service.ILogService;
import top.lenjon.lib.service.IUserDetailService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @Author whz
 * @Date 2020/11/16 00:09
 **/
@RestController
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    IAdminService iAdminService;
    @Autowired
    ILogService iLogService;
    @Autowired
    IBookService iBookService;
    @Autowired
    IUserDetailService iUserDetailService;

    /**
     * 还书
     *
     * @param bookId
     * @param username
     * @return top.lenjon.lib.model.ResponseModel
     */
    @MailSign(value = "归还")
    @GetMapping("/returnBook")
    public ResponseModel returnBook(int bookId, String username) {
        Book book = iBookService.getById(bookId);
        UserDetail userDetail = iUserDetailService.getUserByUserName(username);
        if(userDetail==null){
            return ResponseModel.error("查无此人");
        }
        //确认是本人归还
        if (userDetail.getId().equals(book.getBorrowerId())) {
            book.setBorrowerId(null);
            book.setLoan(false);
            iBookService.saveOrUpdate(book);

            List<Log> list = iLogService.getLogByUserId(userDetail.getId());
            Log log = null;
            for (Log a : list) {
                if (a.getBookId() == bookId) {
                    log = a;
                    break;
                }
            }
            if (log != null) {
                log.setReturnTime(LocalDateTime.now());
                iLogService.saveOrUpdate(log);
                return ResponseModel.success("归还成功");
            }
        }
        return ResponseModel.error("未借阅此书");
    }

    /**
     * 借书
     *
     * @param bookId
     * @param username
     * @return top.lenjon.lib.model.ResponseModel
     */
    @GetMapping("/borrow")
    public ResponseModel borrowBook(int bookId, String username) {
        Book book = iBookService.getById(bookId);
        UserDetail userDetail = iUserDetailService.getUserByUserName(username);
        if (userDetail == null) {
            return ResponseModel.error("查无此人");
        }
        int unReCount = Optional.ofNullable(iLogService.getUnReturnLogOf(userDetail.getId())).map(List::size).orElse(0);
        if (book.getBorrowerId() == null) {
            if (unReCount <= 5) {
                book.setLoan(true);
                book.setBorrowerId(userDetail.getId());
                iBookService.saveOrUpdate(book);
                Log log = new Log(userDetail.getId(), LocalDateTime.now(), bookId);
                iLogService.save(log);
                return ResponseModel.success("借阅成功");
            } else {
                return ResponseModel.error("借阅次数已达上限，请先归还");
            }
        }
        return ResponseModel.error("书本已借阅");
    }

    @GetMapping("/setStatus")
    public ResponseModel setStatus(int bookId, String status) {
        if (iBookService.setBookStatus(bookId, status)) {
            return ResponseModel.success("ok");
        } else {
            return ResponseModel.error("无此书");
        }
    }
}
